package org.ninjarope.cinderstained.main.engine;

import java.util.HashMap;

import org.ninjarope.cinderstained.main.gameplay.definition.pool.Pool;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public abstract class Game implements ApplicationListener {		
	protected static Game instance;
	
	public static Game get() {
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> Pool<T> pools(String name) {
		return (Pool<T>) instance.getPool(name);
	}
	
	protected Screen screen;
	protected HashMap<String, TextureAtlas> atlases = new HashMap<String, TextureAtlas>();
	protected HashMap<String, Screen> screens = new HashMap<String, Screen>();
	
	@SuppressWarnings("rawtypes")
	protected HashMap<String, Pool> pools = new HashMap<String, Pool>();
	
	public void addPool(String name, Pool<?> pool) {
		pools.put(name, pool);
	}
	
	@SuppressWarnings("rawtypes")
	public Pool getPool(String name) {
		return pools.get(name);
	}
	
	public void addScreen(String name, Screen screen) {
		screens.put(name, screen);
	}
	
	public Screen getScreen(String name) {
		return screens.get(name);
	}
	
	public void addAtlas(String name, String file) {
		atlases.put(name, new TextureAtlas(Gdx.files.internal(file)));		
	}
	
	public TextureAtlas getAtlas(String name) {
		return atlases.get(name);
	}
	
	public TextureRegion getFromAtlas(String name, String reg) {
		return atlases.get(name).findRegion(reg);
	}
	
	public void setScreen(Screen scr) {
		setScreen(scr, false);
	}
	
	public void setScreen(Screen scr, boolean destroy) {
		screen.pause();
		if(destroy) {
			screen.dispose();
		}
		screen = scr;
	}

	public abstract Screen init();

	@Override
	public void create() {
		Game.instance = this;
		screen = init();		
	}

	@Override
	public void resume() {
		screen.resume();
	}

	@Override
	public void render() {
		screen.update(Gdx.graphics.getDeltaTime());
		screen.present(Gdx.graphics.getDeltaTime());
	}

	@Override
	public void resize(int width, int height) {

	}

	@Override
	public void pause() {
		screen.pause();
	}

	@Override
	public void dispose() {
		screen.dispose();
	}
}